package com.dhcc.crftool.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dhcc.crftool.entity.CRFFillData;
import com.dhcc.crftool.enums.ResultStatusEnum;
import com.dhcc.crftool.service.CRFFillDataService;
import com.dhcc.crftool.uilts.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author deck
 * @Date 2020/3/2
 */
@Api(value = "CRF_filldata接口", tags="CRF管理filldata")
@RestController
@RequestMapping("/crf/filldata")
public class CRFManageFillDataCotroller {
    @Resource
    private CRFFillDataService crfFillDataService;

    @ApiOperation(value = "查询所有", notes = "查询所有", httpMethod = "GET")
    @GetMapping("/getAll")
    public ResultModel getAll(){
        return ResultModel.ok(crfFillDataService.list());
    }

    @ApiOperation(value="根据条件来查询数据",notes = "根据条件来查询数据",httpMethod = "GET")
    @GetMapping("/getById")
    public ResultModel getById(int id){
        return ResultModel.ok(crfFillDataService.getById(id),"根据id来查询数据成功");
    }

    @ApiOperation(value="根据条件来查询数据",notes = "根据条件来查询数据",httpMethod = "GET")
    @GetMapping("/getByFormId")
    public ResultModel getByFormId(@RequestParam String formId){
        QueryWrapper<CRFFillData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("form_id",formId);
        CRFFillData one = crfFillDataService.getOne(queryWrapper);
        return ResultModel.ok(one,"根据formId来查询数据成功");
    }

    @ApiOperation(value = "添加数据", notes = "添加数据", httpMethod = "POST")
    @PostMapping("/insert")
    public ResultModel insert(@RequestBody CRFFillData crfFillData){
        crfFillDataService.save(crfFillData);
        return ResultModel.ok(null, ResultStatusEnum.CREATED.getCode());
    }

    @ApiOperation(value = "根据id删除数", notes = "根据id删除数", httpMethod = "GET")
    @GetMapping("/delete")
    public ResultModel delete(int id){
        crfFillDataService.removeById(id);
        return ResultModel.ok(null,ResultStatusEnum.NO_CONTENT.getCode());
    }
    @ApiOperation(value = "根据ids删除数据", notes = "根据ids批量删除数据", httpMethod = "GET")
    @GetMapping("/deleteBatchIds")
    public ResultModel deleteBatchIds(int[] idList){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < idList.length; i++) {
            list.add(idList[i]);
        }
        crfFillDataService.removeByIds(list);
        return ResultModel.ok();
    }
    @ApiOperation(value = "根据id修改数据", notes = "根据id修改数据", httpMethod = "POST")
    @PostMapping("/update")
    public ResultModel update(@RequestBody CRFFillData crfFillData){
        int id = crfFillData.getId();
        UpdateWrapper<CRFFillData> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        crfFillDataService.update(crfFillData,updateWrapper);
        return ResultModel.ok();
    }
}
